<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/6/5
 * Time: 15:35
 */

namespace app\educational\controller;

use app\classes\model\ClassModel;
use app\educational\model\TeacherHomeworkModel;
use app\portal\model\UserModel;
use cmf\controller\AdminBaseController;
use think\Db;

class HomeworkController extends AdminBaseController
{
    public function index()
    {
        $admin_shop_id = $this->admin_shop_id;
        $param = $this->request->param();
        //店铺ID
        $param['admin_shop_id'] = $admin_shop_id;

        $teacher_homework = new TeacherHomeworkModel();
        $data = $teacher_homework->getList($param);

        //所有的班级
        $class = Db::name('class')->where(['delete_time' => 0])->select()->toArray();
        //处理内容输出
        foreach ($data as $k => $v) {
            $v['content'] = trim(htmlspecialchars_decode($v['content']), '');
            $data[$k] = $v;
        }
        $current_id = cmf_get_current_admin_id();

        //角色
        $role_id = Db::name('role_user')->alias('c')
            ->join('role r', 'r.id=c.role_id')
            ->where(['user_id' => $current_id])->value('r.id');
        //出去所有的老师
        $all_teacher = Db::name('user')->alias('u')->field('u.*')
            ->join('role_user r', 'r.user_id=u.id')
            ->where(['u.user_status' => 1, 'r.role_id' => 4])
            ->select()
            ->toArray();

        $class_all = $teacher_homework->class_all();

        $this->assign('role_id', $role_id);
        $this->assign('all_teacher', $all_teacher);
        $this->assign('class', $class);
        $this->assign('data', $data);
        $this->assign('class_all', $class_all);
        $this->assign('param', $param);
        $this->assign('page', $data->render());
        return $this->fetch();
    }

    /**
     * ajax 班级后课程进度改变了
     */
    public function changeLesson()
    {
        $id = $this->request->param('id');
        $class_model = new ClassModel();
        $data = $class_model->where(['teacher_id' => $id])->select()->toArray();
        return json_encode($data);
    }

    /**
     * ajax 班级后课程进度改变了
     */
    public function changProgress()
    {
        $id = $this->request->param('id');
        $class_model = new ClassModel();
        $data = $class_model->progress($id);
        return $data;
    }

    public function addPost()
    {
        if ($this->request->isPost()) {
            $post = $this->request->post();

            $post['create_time'] = time();
            $post['update_time'] = time();
            $teacher_homework = new TeacherHomeworkModel();
            //验证班级
            $result = $this->validate($post, 'Homework');
            if ($result !== true) {
                $this->error($result);
            }
            $res = $teacher_homework->insert($post);
            if ($res) {
                $id = $teacher_homework->getLastInsID();
                $pk = $id; //主键
                $table = 'teacher_homework';//数据表名
                if ($id) {
                    //记录日志
                    $teacher_name = db::name("user")->where('id', cmf_get_current_admin_id())->value('user_login');
                    $content = lang('ADD_HOMEWORK', ['name' => $teacher_name]);
                    do_log($content, $pk, $table, time());
                    return $this->success('添加作业成功');
                }
            } else {
                return $this->error('添加作业失败');
            }
        }
    }

    public function edit()
    {
        $id = $this->request->param('id');
        $teacher_homework = new TeacherHomeworkModel();
        $data = $teacher_homework->where(['id' => $id])->find()->toArray();
        $data['content'] = trim(htmlspecialchars_decode($data['content']), '');
//        var_dump(trim(htmlspecialchars_decode($data['content']), ''));
        return json_encode($data);
    }

    public function editPost()
    {
        if ($this->request->isPost()) {
            $id = $this->request->param('id');
            $post = $this->request->post();
            $teacher_homework = new TeacherHomeworkModel();
            //当前用户的id
            $current_id = cmf_get_current_admin_id();
            $role_id = Db::name('role_user')->alias('c')
                ->join('role r', 'r.id=c.role_id')
                ->where(['user_id' => $current_id])->value('r.id');
            //用户老师的话
            if ($role_id == 4) {
                $result = $this->validate($post, 'Homework');
                if ($result !== true) {
                    $this->error($result);
                }
            }
            $teacher_homework = new TeacherHomeworkModel();
            $post['update_time'] = time();
            $res = $teacher_homework->allowField(true)->isUpdate(true)->data($post, true)->save(['id' => $id]);
            if ($res) {
                $pk = $id; //主键
                $table = 'teacher_homework';//数据表名
                //记录日志
                $teacher_name = db::name("user")->where('id', cmf_get_current_admin_id())->value('user_login');
                $content = lang('EDIT_HOMEWORK', ['name' => $teacher_name]);
                do_log($content, $pk, $table, time());
                return $this->success('编辑作业成功', url('Homework/index'));
            } else {
                return $this->error('编辑作业失败', url('Homework/index'));
            }
        }
    }

    /*
    * 作业的删除
    */
    public function delete()
    {


        $teacher_homework = new TeacherHomeworkModel();
        $ids = $this->request->param('ids/a');
        $id = $this->request->param('id', 0, 'intval');

        if (!is_array($ids)) {

            Db::startTrans();
            try {
                $res = $teacher_homework->where('id', intval($id))->update(['delete_time' => time()]);

                $pk = $id; //主键
                $table = 'teacher_homework';//数据表名
                //记录日志
                $teacher_name = db::name("user")->where('id', cmf_get_current_admin_id())->value('user_login');
                $content = lang('DELETE_HOMEWORK', ['name' => $teacher_name]);
                do_log($content, $pk, $table, time());

                Db::commit();
                $this->success('删除作业成功');
            } catch (Exception $e) {
                Db::rollback();
                $this->error('删除作业失败');
            }
        } else {
            $cur_time = time();
            if (is_array($ids)) {
                //批量删除
                Db::startTrans();
                try {
                    $teacher_homework->where(['id' => ['in', $ids]])->update(['delete_time' => $cur_time]);
                    $pk = implode(',', $ids);
                    $table = 'teacher_homework';//数据表名
                    //记录日志
                    $teacher_name = db::name("user")->where('id', cmf_get_current_admin_id())->value('user_login');
                    $content = $teacher_name . '批量删除作业';
                    do_log($content, $pk, $table, time());

                    Db::commit();
                    $this->success('删除成功');
                } catch (Exception $e) {
                    Db::rollback();
                    $this->error('删除失败');
                }
            }
        }

    }
}